package com.example.smartcity4.ui.service;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import com.example.smartcity4.MainActivity;
import com.example.smartcity4.R;
import com.example.smartcity4.adapter.ServiceAdapter;
import com.example.smartcity4.javaBean.GsonBean;
import com.example.smartcity4.reteofi.SmartService;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class serviceFragment extends Fragment {
    private View view;
    private TextView title;
    private ImageView left;
    private SearchView search;
    private ListView list;
    private GridView grid;
    private String string[] = new String[]{"全部服务", "车主服务", "生活服务", "便民服务"};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_service, container, false);
        initView();
        return view;
    }

    private void initView() {


        title = view.findViewById(R.id.title);
        left = view.findViewById(R.id.left);
        search = view.findViewById(R.id.search);
        list = view.findViewById(R.id.list);
        grid = view.findViewById(R.id.grid);
        title.setText("全部服务");
        initData();


    }

    private void initData() {
        serviceType();
        serviceName();
    }

    private void serviceName() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(MainActivity.host)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        final SmartService service = retrofit.create(SmartService.class);

        Call<GsonBean> call = service.get_service();
        call.enqueue(new Callback<GsonBean>() {
            @Override
            public void onResponse(Call<GsonBean> call, final Response<GsonBean> response) {
                grid.setAdapter(new ServiceAdapter(response.body().rows, getContext()));
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        ArrayList<GsonBean.Rows> serviceData = new ArrayList<>();
                        if (position == 0) {
                            grid.setAdapter(new ServiceAdapter(response.body().rows, getContext()));

                        } else {
                            for (int i = 0; i < response.body().rows.size(); i++) {
                                if (response.body().rows.get(i).serviceType.equals(string[position])) {
                                    serviceData.add(response.body().rows.get(i));
                                }
                            }
                        }
                        grid.setAdapter(new ServiceAdapter(serviceData, getContext()));
                    }

                });
                //
            }

            @Override
            public void onFailure(Call<GsonBean> call, Throwable throwable) {

            }
        });

    }

    private void serviceType() {
        list.setAdapter(new ArrayAdapter(getContext(), R.layout.support_simple_spinner_dropdown_item, string));

    }
}